package com.css.fxfzmhwz.base.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzmhwz.base.response.RestResponse;
import com.css.fxfzmhwz.base.user.service.UserService;
import com.css.fxfzmhwz.base.user.viewobjects.UserQueryParams;
import com.css.fxfzmhwz.base.user.viewobjects.UserVO;
import com.css.fxfzmhwz.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 游客登录 游客用户表，游客密码表
 */
@RestController
@RequestMapping("/fxfz/user")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping
    public RestResponse<UserVO> saveUser(@RequestBody UserVO userVO) {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(userService.saveUser(userVO));
        }catch (Exception e){
            log.error("新增用户失败!",e);
            restResponse = RestResponse.fail("新增用户失败!");
        }
        return restResponse;
    }

    //查询所有用户
    @GetMapping("/getAllUser")
    public RestResponse<JSONObject> getUserByParams(HttpServletRequest request, UserQueryParams queryParams) {
        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            queryParams.setCurPage(curPage);
            queryParams.setPageSize(pageSize);
            restResponse = RestResponse.succeed(userService.getAllUserByParams(queryParams));
        }catch (Exception e){
            log.error("查询用户失败!",e);
            restResponse = RestResponse.fail("查询用户失败");
        }
        return restResponse;
    }

    //根据id查询用户
    @GetMapping("{id}")
    public RestResponse<UserVO> getUser(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(userService.getUser(id));
        }catch (Exception e){
            log.error("获取用户失败!",e);
            restResponse = RestResponse.fail("获取用户失败!");
        }
        return restResponse;
    }

    //修改用户

    @PostMapping("updateUser")
    public RestResponse<UserVO> updateUser(@RequestBody UserVO userVO)  {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(userService.updateUser(userVO));
        }catch (Exception e){
            log.error("修改用户失败!",e);
            restResponse = RestResponse.fail("修改用户失败!");
        }
        return restResponse;
    }

    /**
     * 逻辑删除，修改数据标志位
     * @param ids
     * @return
     */
    @PatchMapping("{ids}")
    public RestResponse modityUser(@PathVariable("ids") List<String> ids) {
        RestResponse restResponse = null;
        try{
            userService.deleteUsers(ids);
            restResponse = RestResponse.succeed();
        }catch (Exception e){
            log.error("删除用户失败!",e);
            restResponse = RestResponse.fail("删除用户失败!");
        }
        return restResponse;
    }

    //修改密码

    //查询未激活用户
    //激活用户（在新增用户时设置为已激活了）

    //查询锁定用户
    //解锁用户

    //重置密码

}
